import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

typedef WidgetBuilder = Widget Function(BuildContext context);

class MovableView extends StatefulWidget {

  final double headMoveMaxHeight;
  final double trailMoveMaxHeight;
  final double moveFactor;
  final Color backgroundColor;
  final WidgetBuilder widgetBuilder;

  const MovableView({required this.headMoveMaxHeight, required this.trailMoveMaxHeight,
    required this.backgroundColor, this.moveFactor = 0.02, required this.widgetBuilder, super.key});

  @override
  State<StatefulWidget> createState() => _MovableState();

}

class _MovableState extends State<MovableView> {

  double _headMoveHeight = 0;
  double _tailMoveHeight = 0;
  double _moveStartY = 0;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      child: Column(children: [
      Container(
        // color: widget.backgroundColor,
        height: _headMoveHeight,
        decoration: BoxDecoration(
          color: widget.backgroundColor,
          border: Border(top: BorderSide(color: Colors.grey.withOpacity(0.2)))),
      ),
      Expanded(child: Transform.translate(offset: Offset(0, _tailMoveHeight), child: widget.widgetBuilder(context),),)
    ],), onVerticalDragDown: (event) {
      print('GestureDetector down!!!!!!!');
      _moveStartY = event.globalPosition.dy;
    }, onVerticalDragStart: (event) {
      print('GestureDetector start!!!!!!!');
      _moveStartY = event.globalPosition.dy;
    }, onVerticalDragUpdate: (event) {
      print('GestureDetector update !!!!!!!');
      var moveHeight = event.globalPosition.dy - this._moveStartY;
      print('GestureDetector update!!!!!!!:${moveHeight}');
      if (moveHeight > widget.headMoveMaxHeight) {
        moveHeight = widget.headMoveMaxHeight + moveHeight * widget.moveFactor;
      } else if (moveHeight < -widget.trailMoveMaxHeight) {
        moveHeight = -widget.headMoveMaxHeight + moveHeight * widget.moveFactor;
      }
      setState(() {
        if (moveHeight < 0) {
          _tailMoveHeight = moveHeight;
        } else {
          _headMoveHeight = moveHeight;
        }
      });
    }, onVerticalDragEnd: (event) {
      setState(() {
        _headMoveHeight = 0;
        _tailMoveHeight = 0;
      });
    },);
  }

}